package com.yiwei.jmx.view;

/**
 * Created by xsooy-pc on 18-7-27.
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.yiwei.jmx.R;


public class MyProgressView extends View {
    private float fArcNum;
    private float fMax;
    private float density;
    private float nowProgress;
    private RectF rect;
    private Paint paint;
    private Paint paintFinish;
    private int strokeWidth;
    private int[] mColors;
    private int unFinishColor;
    private int finishColor;

    public float getDensity() {
        return density;
    }

    public void setDensity(float density) {
        this.density = density;
    }

    public MyProgressView(Context context) {
        super(context);
        init(context, null);
    }

    public MyProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MyProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        paint = new Paint();

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyProgressView);
        nowProgress = a.getFloat(R.styleable.MyProgressView_ring_now_progress, 0f);
        fMax = a.getFloat(R.styleable.MyProgressView_ring_max_progress, 0f);
        density = a.getDimensionPixelSize(R.styleable.MyProgressView_ring_width, 90);
        strokeWidth = a.getDimensionPixelSize(R.styleable.MyProgressView_ring_stroke_width, 10);
        unFinishColor = a.getColor(R.styleable.MyProgressView_ring_color_unfinish, 0);
        finishColor = a.getColor(R.styleable.MyProgressView_ring_color_finish, 0);
        paint.setStrokeWidth(strokeWidth);
        paint.setStyle(Paint.Style.STROKE);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        if (unFinishColor != 0) {
            paint.setColor(unFinishColor);
        } else {
            paint.setColor(0xffDDDDDD);
        }


        paintFinish = new Paint();
        paintFinish.setStrokeWidth(strokeWidth);
        paintFinish.setStyle(Paint.Style.STROKE);
        paintFinish.setFlags(Paint.ANTI_ALIAS_FLAG);
        if (finishColor != 0) {
            paintFinish.setColor(finishColor);
        } else {
            mColors = new int[]{
                    0xFF0072FF,//紫色
                    0xFF00AEFF,//褐色

            };
            Shader shader = new SweepGradient(getMeasuredWidth() / 2, getMeasuredHeight() / 2, mColors, null);
            paintFinish.setShader(shader);
        }


        setProgress(nowProgress);
        rect = new RectF();
        a.recycle();
    }

    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        if (fArcNum >= 0) {
//            未完成进度


            canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, getMeasuredWidth() / 2 - strokeWidth, paint);
        }
//        已完成进度


        //paint.setStrokeWidth(2);

//        背景
        rect.set(0 + strokeWidth, 0 + strokeWidth, getMeasuredWidth() - strokeWidth, getMeasuredWidth() - strokeWidth);
        canvas.drawArc(rect, -90, fArcNum, false, paintFinish);
//        paint.setColor(0xe6ffffff);
//        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
//        canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2, density / 2 - 5, paint);
    }

    public void setProgress(float num) {
        fArcNum = (num / fMax) * 360;
    }


    public void setProgressSec(float num, float max) {
        fArcNum = (num / max) * 360;
        invalidate();
    }

    public void setProgressSec(float progress) {
        fArcNum = progress * 360;
        invalidate();
    }

    public float getfArcNum() {
        return fArcNum;
    }

    public void setfArcNum(float fArcNum) {
        this.fArcNum = fArcNum;
    }

    public float getfMax() {
        return fMax;
    }

    public void setfMax(float fMax) {
        this.fMax = fMax;
    }
}
